{*******************************************************************************
 *                                                                             *
 *          Mathengine                                                         *
 *                                                                             *
 *          (C)Copyright Jimx 2011                                             *
 *                                                                             *
 *          http://math-engine.googlecode.com                                  *
 *                                                                             *
 *******************************************************************************
 *  This file is part of Mathengine.                                           *
 *                                                                             *
 *  Mathengine is free software:                                               *
 *  you can redistribute it and/or modify                                      *
 *  it under the terms of the GNU General Public License as published by       *
 *  the Free Software Foundation, either version 3 of the License, or          *
 *  (at your option) any later version.                                        *
 *                                                                             *
 *  Mathengine is distributed                                                  *
 *  in the hope that it will be useful,                                        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 *  GNU General Public License for more details.                               *
 *                                                                             *
 *  You should have received a copy of the GNU General Public License          *
 *  along with Mathengine.                                                     *
 *  If not, see <http://www.gnu.org/licenses/>.                                *
 *******************************************************************************}
unit sci.log;

interface

uses
  SysUtils;

type
  TLog = record
    fpath: string; (* XXX *)
  end;

var
  dbgfile: TextFile;

procedure dbginit(path: string);
procedure loginit(var l: TLog; path: string);
procedure closefiles(var l: TLog);

procedure error(msg: string);

procedure dbgprint(s: string);
procedure dbgprintln(s: string);

procedure log(l: TLog; s: string);

implementation

procedure dbginit(path: string);
begin
  AssignFile(dbgfile, path);
  Rewrite(dbgfile);
end;

procedure loginit(var l: TLog; path: string);
var
  f: text;
begin
  l.fpath := path;
  AssignFile(f, l.fpath);
  Rewrite(f);
  CloseFile(f);
end;

procedure closefiles(var l: TLog);
begin
end;

procedure error(msg: string);
begin
  raise Exception.Create(msg);
end;

procedure dbgprint(s: string);
begin
  write(dbgfile, s);
end;

procedure dbgprintln(s: string);
begin
  writeln(dbgfile, s);
end;

function gettime: string;
begin
  Result := DateTimeToStr(Now);
end;

procedure log(l: TLog; s: string);
var
  f: text;
begin
  AssignFile(f, l.fpath);
  if not FileExists(l.fpath) then Rewrite(f)
  else Append(f);
  writeln(f, format('[%s]: %s', [gettime, s]));
  CloseFile(f);
end;

end.
